package com.hopu.v1.service.admin.subject.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.exception.BusinessException;
import com.hopu.v1.mapper.admin.subject.ClassMapper;
import com.hopu.v1.mapper.admin.system.StaffSubjectMapper;
import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.system.Class;
import com.hopu.v1.pojo.system.Role;
import com.hopu.v1.pojo.system.StaffSubject;
import com.hopu.v1.service.admin.subject.ClassService;
import com.hopu.v1.service.admin.system.StaffService;
import com.hopu.v1.vo.ClassVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Resource
    private ClassMapper classMapper;

    @Autowired
    private StaffSubjectMapper staffSubjectMapper;
    /**
     * 获取班级列表
     * @return
     */
    @Override
    public PageData<StaffSubject> selectAll(Page<Class> page, String className, String realName, String teacherName, String subjectName,Integer classStatus,Integer staffId) {
        System.out.println(classStatus);
        // 计算偏移量
        int offset = (int) ((page.getCurrent() - 1) * page.getSize());
        // 获取分页数据
        List<StaffSubject> classVoList = staffSubjectMapper.classSubject((long) offset, page.getSize(), className, realName, teacherName,subjectName, classStatus,staffId);
        // 获取总数
        Integer total = staffSubjectMapper.getTotal(className, realName, teacherName,subjectName,classStatus);

        // 封装 PageData
        PageData<StaffSubject> pageData = new PageData<>((int)page.getPages(),(int)page.getSize(),total,classVoList);
        return pageData;
    }

    /**
     * 添加班级
     */
    @Override
    public void insert(Class class1){
        LambdaQueryWrapper<Class> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Class::getClassName, class1.getClassName());
        Class class2 = getOne(lambdaQueryWrapper);
        if (class2 != null ) {
            throw new BusinessException("班级已存在");
        }
        if (class1.getStaffId() == null) {
            throw new BusinessException("输入教员名");
        }
        if (class1.getTeacherId() == null){
            throw new BusinessException("输入班主任名");
        }
        if (class1.getSubjectId() == null){
            throw new BusinessException("输入学科名");
        }
        System.out.println(class1.getTeacherId()==class1.getStaffId());
        if (class1.getTeacherId()==class1.getStaffId()){
            throw new BusinessException("教员和班主任不能重复");
        }
        //插入用户
        save(class1);
    }

   /**
     * 根据id获取班级内学生人数
     */
    @Override
    public Integer getNumberById(Integer classId) {
        return classMapper.getNumberById(classId);
    }

    /**
     * 修改状态创建结业的时间
     *
     * @param id
     */
    @Override
    public void updateClassStatus(Integer id, Integer classStatus,LocalDate endDate) {
        classMapper.updateById(new Class().setId(id).setClassStatus(0).setEndDate(endDate));
    }

    /**
     * 修改班级
     * @param class1
     */
    @Override
    public void update(Class class1,String className) {
        if (className.equals(class1.getClassName())) {
            class1.setClassName(null);
        }
        LambdaQueryWrapper<Class> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Class::getClassName, class1.getClassName());
        Class class2 = getOne(lambdaQueryWrapper);
        if (class2 != null) {
            throw new BusinessException("班级已存在");
        }
        updateById(class1);
    }

    /**
     *根据班级id查询班级信息
     * @param classId
     * @return
     */
    @Override
    public Class getClassInfo(Integer classId) {
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classLambdaQueryWrapper.eq(Class::getId,classId);
        Class one = getOne(classLambdaQueryWrapper);
        return one;
    }
}
